﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using CSBackup_Web.Sources.Legacy;

namespace CSBackup_Web.Sources.Override
{
    /// <summary>
    /// C#重写原C++的ToolConfigFile类
    /// </summary>
	public class ToolConfigFile
	{
        public static readonly int FILE_LIST_START_LINE = 10;
        /// <summary>
        /// 读取配置文件第n行（注意不是“前”n行）
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="n">第几行</param>
        /// <returns>该行字符串或空字符串（不存在该行）或null（出错）</returns>
        public static string ReadLineN(string fileName, int n)
        {
            if ((null == fileName) || (string.Empty == fileName))
                return null;

            if (n <= 0)
                return null;

            string line = string.Empty;
            using (FileStream fs = File.OpenRead(fileName))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                {
                    for (int i = 0; i < n; ++i)
                    {
                        if (null == (line = sr.ReadLine()))
                        {
                            break;
                        }
                    }
                }
            }

            //if (null == line)
            //    return string.Empty;
            return line;
        }

        public static string LoadProjectName(string fileName)
        {
            string name = string.Empty;
            if ((name = ReadLineN(fileName, 1)) != null)
                return name;
            return string.Empty;
        }

        public static string LoadFullBackupPath(string fileName)
        {
            string path = string.Empty;
            if ((path = ReadLineN(fileName, 2)) != null)
                return path;
            return string.Empty;
        }

        public static string LoadIncBackupPath(string fileName)
        {
            string path = string.Empty;
            if ((path = ReadLineN(fileName, 3)) != null)
                return path;
            return string.Empty;
        }

        public static string LoadDiffBackupPath(string fileName)
        {
            string path = string.Empty;
            if ((path = ReadLineN(fileName, 4)) != null)
                return path;
            return string.Empty;
        }

        public static string LoadDiffIncBackupPath(string fileName)
        {
            string path = string.Empty;
            if ((path = ReadLineN(fileName, 5)) != null)
                return path;
            return string.Empty;
        }

        public static int LoadAutoCycle(string fileName)
        {
            string ret = ReadLineN(fileName, 6);
            if (ret != null)
                return int.Parse(ret);
            return 0;
        }

        public static int LoadAutoHour(string fileName)
        {
            string ret = ReadLineN(fileName, 7);
            if (ret != null)
                return int.Parse(ret);
            return 0;
        }

        public static int LoadAutoMin(string fileName)
        {
            string ret = ReadLineN(fileName, 8);
            if (ret != null)
                return int.Parse(ret);
            return 0;
        }

        public static int LoadIsAutoBackupOnce(string fileName)
        {
            string ret = ReadLineN(fileName, 9);
            if (ret != null)
                return int.Parse(ret);
            return 0;
        }

        public static int SaveConfigFile(string fileName, string prjName,
            string fBkpPath, string iBkpPath, string dBkpPath, string diBkpPath,
            int autoCycle, int autoHour, int autoMin, bool isAutoBackupOnce,
            List<string> fileList)
        {
            if ((null == fileName) || (null == prjName)
                || (null == fBkpPath) || (null == iBkpPath)
                || (null == dBkpPath) || (null == diBkpPath)
                || (null == fileList))
            {
                return 1; //参数错误
            }

            using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.Default))
            {
                sw.WriteLine(prjName);
                sw.WriteLine(fBkpPath);
                sw.WriteLine(iBkpPath);
                sw.WriteLine(dBkpPath);
                sw.WriteLine(diBkpPath);
                sw.WriteLine(autoCycle.ToString());
                sw.WriteLine(autoHour.ToString());
                sw.WriteLine(autoMin.ToString());
                sw.WriteLine((isAutoBackupOnce) ? ((1).ToString()) : ((0).ToString()));

                foreach (string line in fileList)
                {
                    sw.WriteLine(line);
                }
            }

            return 0;
        }

        public static int CheckBackupPaths(string full, string inc, string diff, string diffInc)
        {
            if (!ToolFileOperation.IsValidPath(full))
                return 1;
            if (!ToolFileOperation.IsValidPath(inc))
                return 2;
            if (!ToolFileOperation.IsValidPath(diff))
                return 3;
            if (!ToolFileOperation.IsValidPath(diffInc))
                return 4;

            bool res = ((full != inc) && (full != diff) && (full != diffInc));
            if (!res)
                return 1;
            res = ((inc != diff) && (inc != diffInc));
            if (!res)
                return 2;
            res = (diff != diffInc);
            if (!res)
                return 3;

            return 0;
        }

        /// <summary>
        /// 从工程文件装载工程信息
        /// </summary>
        /// <param name="prj">工程实例引用</param>
        /// <param name="filePath">工程文件全路径</param>
        /// <returns>0 -- 成功 ；1 -- 参数错误 ；2 -- 找不到文件 ；3 -- 文件内容错误</returns>
        public static int LoadPrjFromFile(ProjectInfo prj, string filePath)
        {
            if (null == prj)
                return 1;
            if ((null == filePath) || (string.Empty == filePath))
                return 1;

            if (!File.Exists(filePath))
                return 2;

            int ls = FILE_LIST_START_LINE;
            string line = string.Empty;
            prj.BkpPrjFile = filePath;
            try
            {
                prj.PrjLocation = Directory.GetParent(filePath).FullName;
                prj.PrjName = LoadProjectName(filePath);
                prj.FullBkpPath = LoadFullBackupPath(filePath);
                prj.IncBkpPath = LoadIncBackupPath(filePath);
                prj.DiffBkpPath = LoadDiffBackupPath(filePath);
                prj.DiffIncBkpPath = LoadDiffIncBackupPath(filePath);
                prj.AutoCycle = LoadAutoCycle(filePath);
                prj.AutoHour = LoadAutoHour(filePath);
                prj.AutoMin = LoadAutoMin(filePath);
                prj.IsAutoBackupOnce = (LoadIsAutoBackupOnce(filePath) == 1);
                //装载待备份文件列表
                while ((line = ReadLineN(filePath, ls)) != null)
                {
                    prj.CurrentBackup.FileList.Add(line);
                    ++ls;
                }
                //检查四种备份类型的路径
                if (CheckBackupPaths(prj.FullBkpPath, prj.IncBkpPath,
                    prj.DiffBkpPath, prj.DiffIncBkpPath) != 0)
                    return 3;
                //装载四种备份类型的已备份列表
                ToolBackupInfo.LoadFullBkpInfoList(prj.FullBkpPath, prj.FullBackupList);
                ToolBackupInfo.LoadIncBkpInfoList(prj.IncBkpPath, prj.IncBackupList);
                ToolBackupInfo.LoadDiffBkpInfoList(prj.DiffBkpPath, prj.DiffBackupList);
                ToolBackupInfo.LoadDiffIncBkpInfoList(prj.DiffIncBkpPath, prj.DiffIncBackupList);
            }
            catch (Exception exp)
            {
                //在这里可以添加信息到日志面板
                Debug.WriteLine("Exp in LoadPrjFromFile(): " + exp.Message);
                return 3;
            }

            return 0;
        }
	}//end of class
}
